package com.twu.biblioteca;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class BookData {

    private List<String> bookTitleList = new ArrayList<String>();
    private List<Integer> bookQuantityList = new ArrayList<Integer>();
    private File dataFile = new File("src/com/twu/biblioteca/book.txt"); // default file path

    public void setBookDataFile(File filePath) throws IOException {
        dataFile = filePath;
        dataFile.createNewFile();
    }

    public void readBookData() throws IOException {
        bookTitleList.clear();
        bookQuantityList.clear();
        BufferedReader fileReader = new BufferedReader(new FileReader(dataFile));
        String dataLine;
        while((dataLine = fileReader.readLine())!=null) {
            String[] tempBookAndQuantity = dataLine.split(" ");
            bookTitleList.add(tempBookAndQuantity[0].trim());
            bookQuantityList.add(new Integer(tempBookAndQuantity[1].trim()));
        }
        fileReader.close();
    }

    public void writeBookData() throws IOException {
        BufferedWriter fileWriter = new BufferedWriter(new FileWriter(dataFile));
        for(int i=0;i< bookTitleList.size();i++){
            fileWriter.write(bookTitleList.get(i)+" "+ bookQuantityList.get(i)+"\n");
        }
        fileWriter.close();
    }

    public void addBook(String bookTitle, int quantities) throws IOException {
        readBookData();
        bookTitleList.add(bookTitle);
        bookQuantityList.add(new Integer(quantities));
        writeBookData();
    }

    public boolean deleteBook(String bookTitle) throws IOException {
        readBookData();
        if(!bookTitleList.contains(bookTitle))
            return false;
        int index = bookTitleList.indexOf(bookTitle);
        bookTitleList.remove(index);
        bookQuantityList.remove(index);
        writeBookData();
        return true;
    }

    public BookData getBookData() throws IOException {
        readBookData();
        return this;
    }

    public void putBookData(BookData bookData) throws IOException {
        bookTitleList = bookData.bookTitleList;
        bookQuantityList = bookData.bookQuantityList;
        writeBookData();
    }

    public void printBookData() throws IOException {
        readBookData();
        System.out.println("\nBook-ID ) Book-name [ Book-count ]");
        for(int i=0;i< bookTitleList.size();i++){
            System.out.println(""+(i+1)+") "+bookTitleList.get(i).replace("_"," ")+" [ "+ bookQuantityList.get(i)+" ]");
        }
    }

    public static void main(String args[]) throws IOException {
        BookData dummyBooks = new BookData();
        dummyBooks.readBookData();
        dummyBooks.addBook("XYZ", 12);
        dummyBooks.readBookData();
        dummyBooks.printBookData();
        if(!dummyBooks.deleteBook("XYZ"))
            System.out.println("No Help!");
        dummyBooks.readBookData();
        dummyBooks.printBookData();
    }

    public int getBookQuantity(int bookId) {
        if(bookId > 0)
            return bookQuantityList.get(bookId-1);
        return 0;
    }

    public void putBookQuantity(int bookId, int bookQuantity) {
        bookQuantityList.set(bookId-1, bookQuantity);
    }

    public int getMaxBookId() {
        return bookQuantityList.size();
    }
}
